package com.smzd.framework.system.actions;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.business.MeetingService;
import com.smzd.framework.system.business.ModuleService;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.system.domain.Module;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.utils.JsonHelper;
import com.smzd.framework.utils.db.RPage;

@Controller
@RequestMapping("/system/modules")
@Access(Role.TAG_ROLE_MODULES)
public class ModuleController extends AbstractController<Module> {
    @Autowired
    private ModuleService service;
    @Autowired
    private MeetingService meetingService;

    @Override
    protected IFGService<Module> getService() {
        return service;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, Module elem, BindingResult result) throws Exception {
        return super.toAddPage(model, elem, result);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, Module elem, BindingResult result) throws Exception {
        return super.postAddData(model, elem, result);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, Module elem, BindingResult result) throws Exception {
        return super.toListPage(model, elem, result);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Access({ Role.TAG_ROLE_MODULES, Role.TAG_ROLE_MENUS })
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model, RPage<Module> page, BindingResult result) throws Exception {
        return super.getListData(model, page, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, Module elem, BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, elem, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, Module elem, BindingResult result) throws Exception {
        return super.postEditData(model, elem, result);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, Module elem, BindingResult result) throws Exception {
        return super.postRemoveData(model, elem, result);
    }

    // /////////////扩展功能--会务与功能模块的多对多编辑////////////////////
    /**
     * toMeetingModulesPage：导航到目标用户所属用户组的编辑页面。<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param meetingId
     * @return
     */
    @RequestMapping(value = "/meeting-{meetingId}", method = RequestMethod.GET)
    @Access(Role.TAG_ROLE_MEETING)
    public String toMeetingModulesPage(Map<String, Object> model, @PathVariable("meetingId") int meetingId)
            throws Exception {
        Meeting meeting = meetingService.getItemById(meetingId);
        model.put("item", meeting);
        return getMyView("meetingmodules.ftl");
    }

    /**
     * getMeetingModulesData：获取目标用户的用户组配置列表。<br>
     * 目标用户所属的用户组被排在前面，并且以复选框标志已经属于当前用户组<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param userId
     * @param page
     * @param result
     * @return
     */
    @RequestMapping(value = "/list/meeting-{meetingId}", method = RequestMethod.POST)
    @ResponseBody
    @Access(Role.TAG_ROLE_MEETING)
    public String getMeetingModulesData(Map<String, Object> model, @PathVariable("meetingId") int meetingId,
            RPage<Module> page, BindingResult result) throws Exception {
        page.setExt(String.valueOf(meetingId));
        return service.getMeetingGroupsListDataJson(page);
    }

    /**
     * saveUserGroups：保存用户所属用户组的改变情况。<br>
     * 只有新改变的部分数据需要提交服务器端进行处理。<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param userId
     * @param groupIds
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit/meeting-{meetingId}", method = RequestMethod.POST)
    @ResponseBody
    @Access(Role.TAG_ROLE_MEETING)
    public String saveUserGroups(Map<String, Object> model, @PathVariable("meetingId") int meetingId,
            @RequestParam("moduleIds") String moduleIds) throws Exception {
        try {
            if (StringUtils.isEmpty(moduleIds)) {
                return JsonHelper.getSuccessJsonString("没有任何变动提交！");
            }
            service.saveMeetingModules(meetingId, moduleIds);
            return JsonHelper.getSuccessJsonString("保存成功！");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
